import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;

import java.io.IOException;

//按行查询
public class QueryDemo {
    static Configuration conf = null;

    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.rootdir", "hdfs://192.168.126.139:9000/hbase");
        conf.set("hbase.master", "hdfs://192.168.126.139:60010");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("hbase.zookeeper.quorum", "192.168.126.139");
    }

    public static Result queryByRow(String tableName, String rowKey) throws IOException {
        Get get = new Get(rowKey.getBytes());
        HTable table = new HTable(conf, tableName);
        return table.get(get);
    }

    public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
        //按行查询数据（旧方法)
/*        Result result = queryByRow("student", "rowkey4");
        List<KeyValue> values = result.list();
        System.out.println("COLUMN\t\t\tCELL");
        for (KeyValue value : values) {
            System.out.print(new String(value.getFamily()) + ":");
            System.out.print(new String(value.getQualifier()) + "\t\t");
            System.out.print("value=" + new String(value.getValue()) + ",");
            System.out.println("timestamp=" + value.getTimestamp());
        }*/

        System.out.println("********************新方法********************");
        //按行查询数据（新方法）
        Result result2 = queryByRow("student", "rowkey3");
        Cell[] cells = result2.rawCells();
        System.out.println("COLUMN\t\t\tCELL");
        for (Cell cell : cells) {
            System.out.print(new String(CellUtil.cloneFamily(cell)) + ":");
            System.out.print(new String(CellUtil.cloneQualifier(cell)) + "\t\t");
            System.out.print("value=" + new String(CellUtil.cloneValue(cell)) + ",");
            System.out.println("timestamp=" + cell.getTimestamp());
        }

    }
}
